﻿
package sci.Notification;

import sci.Notification.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity
{
	int notification_id = 19172439;
	NotificationManager nm;
	private static TextView Text = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		Button btnSend = (Button) findViewById(R.id.btnSend);			// 发送通知
		btnSend.setOnClickListener(bt1lis);
		
		Button btnClear = (Button) findViewById(R.id.btnClear);			// 移除通知
		btnClear.setOnClickListener(bt2lis);
		
		Button btnSetting = (Button) findViewById(R.id.btnSetting);		// 设置监听
		btnSetting.setOnClickListener(NOTIFICATION_SETTINGS_listener);
		
		Text = (TextView) findViewById(R.id.textView1);
	}
	
	OnClickListener bt1lis = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			// 发送状态栏通知
			showNotification(R.drawable.home, "图标边的文字", "标题", "内容");
		}
	};
	
	OnClickListener bt2lis = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			// 移除状态栏通知
			nm.cancel(notification_id);
		}
	};
	

	// 设置Notice监听
	OnClickListener NOTIFICATION_SETTINGS_listener = new OnClickListener()
	{
		@SuppressLint("NewApi")
		@Override
		public void onClick(View v)
		{
			// NotificationMonitorService services = new NotificationMonitorService();
			// services.startService(main.getIntent());
			// String string = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
			// if (!string.contains(NotificationListenerService.class.getName()))
			// {
			MainActivity.this.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
			// }
			
		}
	};
	
	/** 显示通知栏消息 */
	public void showNotification(int icon, String tickertext, String title, String content)
	{
		// 设置一个唯一的ID，随便设置
		Notification notification = new Notification(icon, tickertext, System.currentTimeMillis());
		notification.defaults = Notification.DEFAULT_ALL;
		// Notification.DEFAULT_SOUND;
		// Notification.DEFAULT_VIBRATE;
		// Notification.DEFAULT_LIGHTS
		
		PendingIntent pt = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);		// 点击通知后的动作，转回main 这个Acticity
		notification.setLatestEventInfo(this, title, content, pt);	// 设置通知信息
		
		nm.notify(notification_id, notification);					// 显示通知消息
		
	}
	
	
	// 在文本框中显示信息
	public static void showText(final String msg)
	{
		Handler mainHandler = new Handler(Looper.getMainLooper());
		mainHandler.post(new Runnable()
		{
			@Override
			public void run()
			{
				if (Text != null)
				{
					Text.setText(msg);
				}
				else Log.i("showText", msg);
			}
		});
	}
}
